#include "abstractthread.h"

int AbstractThread::totalCount_ = 0;

AbstractThread::AbstractThread(SafeQueue* queue, QObject *parent)
    : QThread(parent)
    , queue_(queue)
    , stopRequested_(false)
    , pauseRequested_(false)
{}

AbstractThread::~AbstractThread()
{
    stopProcessing();
    wait();
}

void AbstractThread::stopProcessing()
{
    stopRequested_.store(true);
    waitCondition_.wakeAll();
}

void AbstractThread::pauseProcessing()
{
    pauseRequested_.store(true);
}

void AbstractThread::keepProcessing()
{
    pauseRequested_.store(false);
    waitCondition_.wakeOne();
}

void AbstractThread::resetProcessCount()
{
    currentIndex_ = 0;
}

void AbstractThread::setIndex(const int index)
{
    currentIndex_ = index;
}

void AbstractThread::setManual(bool isManual)
{
    isManual_ = isManual;
}

bool AbstractThread::getManual()
{
    return isManual_;
}

bool AbstractThread::shouldStop()
{
    return stopRequested_.load();
}

void AbstractThread::checkAndWaitRemovePause()
{
    while (pauseRequested_.load() && !shouldStop())
    {
        QMutexLocker locker(&pauseMutex_);
        waitCondition_.wait(&pauseMutex_, 500);
    }
}

SafeQueue *AbstractThread::getQueue() const
{
    return queue_;
}

void AbstractThread::resetstopFlag()
{
    stopRequested_.store(false);
}
